package com.practice;
//
//import java.io.*;
//
//public class SubFile {
//    public static void main(String[] args) {
//        SubFile sb =new SubFile();
//        for (int i = 0; i <10 ; i++) {
//            Thread thread = new Thread(() -> {
//                synchronized (sb) {
//                    sb.Demo();
//                }
//            });
//            thread.start();
//        }
//
//    }
//
//    public void Demo(){
//        File file = new File("E:\\IO流练习\\sample1.jpg");
//        File dest = new File("E:\\IO流练习\\OutPutStream");
//        long size = 1024 * 1024;
//        if (!dest.getParentFile().exists()) { // 判断父级目录是否存在不存在创建父级目录
//            dest.getParentFile().mkdirs();
//        }
//        try (FileInputStream in = new FileInputStream(file)){
//            byte[] bytes = new byte[(int)size];
//            int readSize ;
//            int i = 1;
//            while ((readSize = in.read(bytes)) != -1) {
//                File outputfile = new File(dest, i + ".mp4");
//                try (FileOutputStream out = new FileOutputStream(outputfile)) {
//                    out.write(bytes, 0, readSize);
//                }
//                i ++;
//            }
//        }catch (IOException e){
//            System.out.println(e.getMessage());
//        }
//    }
//}
import java.io.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SubFile {
    public static void main(String[] args) {
        String inputFile = "E:\\IO流练习\\sample1.jpg"; // 输入文件名
        int numThreads = 4; // 线程数量
        splitFile(inputFile, numThreads);
    }

    public static void splitFile(String inputFile, int numThreads) {
        File file = new File(inputFile);

        if (!file.exists()) {
            System.err.println("文件不存在: " + inputFile);
            return;
        }

        long fileSize = file.length();
        long partSize = fileSize / numThreads;

        try (FileInputStream fis = new FileInputStream(file)) {
            ExecutorService executor = Executors.newFixedThreadPool(numThreads);

            for (int i = 0; i < numThreads; i++) {
                long start = i * partSize;
                long end = (i == numThreads - 1) ? fileSize : start + partSize;
                executor.submit(new FileSplitTask(inputFile, start, end, "part_" + i + ".txt"));
            }

            executor.shutdown();

            System.out.println("文件分割完成！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class FileSplitTask implements Runnable {
    private String inputFile;
    private long start;
    private long end;
    private String outputFileName;

    public FileSplitTask(String inputFile, long start, long end, String outputFileName) {
        this.inputFile = inputFile;
        this.start = start;
        this.end = end;
        this.outputFileName = outputFileName;
    }

    @Override
    public void run() {
        try (RandomAccessFile raf = new RandomAccessFile(inputFile, "r");
             FileOutputStream fos = new FileOutputStream(outputFileName)) {

            raf.seek(start);
            byte[] buffer = new byte[1024];
            int bytesRead;

            while ((bytesRead = raf.read(buffer)) != -1 && raf.getFilePointer() <= end) {
                fos.write(buffer, 0, bytesRead);
            }

            System.out.println("文件分割完成: " + outputFileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

